package com.lifuixng.picturebackend.controller.space;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lifuixng.picturebackend.annotation.AuthCheck;
import com.lifuixng.picturebackend.common.BaseResponse;
import com.lifuixng.picturebackend.common.DeleteRequest;
import com.lifuixng.picturebackend.common.ResultUtils;
import com.lifuixng.picturebackend.constant.UserConstant;
import com.lifuixng.picturebackend.exception.ErrorCode;
import com.lifuixng.picturebackend.exception.ThrowUtils;
import com.lifuixng.picturebackend.model.dto.space.SpaceAddRequest;
import com.lifuixng.picturebackend.model.dto.space.SpaceEditRequest;
import com.lifuixng.picturebackend.model.dto.space.SpaceQueryRequest;
import com.lifuixng.picturebackend.model.dto.space.SpaceUpdateRequest;
import com.lifuixng.picturebackend.model.entity.Space;
import com.lifuixng.picturebackend.model.entity.SpaceLevel;
import com.lifuixng.picturebackend.model.entity.User;
import com.lifuixng.picturebackend.model.enums.SpaceLevelEnum;
import com.lifuixng.picturebackend.model.mapping.SpaceMapping;
import com.lifuixng.picturebackend.model.vo.space.SpaceVO;
import com.lifuixng.picturebackend.model.vo.user.UserVO;
import com.lifuixng.picturebackend.service.SpaceService;
import com.lifuixng.picturebackend.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author Mr.LiFuXing
 * @Date 2024/12/30 21:17
 * @Title SpaceController
 * @Package com.lifuixng.picturebackend.controller
 */

@RestController
@RequestMapping("/space")
@Slf4j
public class SpaceController {

    @Resource
    private SpaceService spaceService;

    @Resource
    private UserService userService;

    @Resource
    private SpaceMapping spaceMapping;

    /**
     * 获取空间列表
     *
     * @param spaceQueryRequest
     * @return
     */
    @GetMapping("/list/page/vo")
    @AuthCheck
    public BaseResponse<Page<SpaceVO>> getSpaceVoListByPage(SpaceQueryRequest spaceQueryRequest) {

        Page<Space> spaceByPage = spaceService.getSpaceByPage(spaceQueryRequest);
        Page<SpaceVO> spaceVOByPage = spaceService.getSpaceVOByPage(spaceByPage);

        return ResultUtils.success(spaceVOByPage);
    }

    /**
     * 获取空间列表
     *
     * @param spaceQueryRequest
     * @return
     */
    @GetMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<Space>> getSpaceListByPage(SpaceQueryRequest spaceQueryRequest) {

        Page<Space> spaceByPage = spaceService.getSpaceByPage(spaceQueryRequest);

        return ResultUtils.success(spaceByPage);
    }


    /**
     * 根据id获取空间信息 管理员     *
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Space> getSpaceById(Long id) {
        ThrowUtils.throwIf(id == null, ErrorCode.PARAMS_ERROR);
        Space space = spaceService.getBaseMapper().selectById(id);

        ThrowUtils.throwIf(ObjUtil.isEmpty(space), ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(space);
    }

    /**
     * 根据id获取空间信息 普通用户     *
     *
     * @param id
     * @return
     */
    @GetMapping("/get/vo")
    @AuthCheck
    public BaseResponse<SpaceVO> getSpaceVOById(Long id) {
        ThrowUtils.throwIf(id == null, ErrorCode.PARAMS_ERROR);
        Space space = spaceService.getBaseMapper().selectById(id);
        ThrowUtils.throwIf(ObjUtil.isEmpty(space), ErrorCode.NOT_FOUND_ERROR);
        // 转换
        SpaceVO spaceVO = spaceMapping.spaceToSpaceVO(space);

        // 填充userVO
        UserVO userVO = userService.getUserVOById(spaceVO.getId());
        spaceVO.setUser(userVO);

        return ResultUtils.success(spaceVO);
    }


    /**
     * 添加空间
     *
     * @param spaceAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    @AuthCheck
    public BaseResponse<Long> addSpace(@RequestBody SpaceAddRequest spaceAddRequest, HttpServletRequest request) {

        User loginUser = userService.userLoginInfo(request);
        Long result = spaceService.addSpace(spaceAddRequest, loginUser);
        return ResultUtils.success(result);
    }


    /**
     * 更新空间
     *
     * @param spaceUpdateRequest
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Integer> updateSpace(@RequestBody SpaceUpdateRequest spaceUpdateRequest) {

        int result = spaceService.updateSpace(spaceUpdateRequest);
        return ResultUtils.success(result);
    }

    /**
     * 编辑空间
     *
     * @param spaceEditRequest
     * @param request
     * @return
     */
    @PostMapping("/edit")
    @AuthCheck
    public BaseResponse<Integer> editSpace(@RequestBody SpaceEditRequest spaceEditRequest, HttpServletRequest request) {

        User user = userService.userLoginInfo(request);
        int result = spaceService.editSpace(spaceEditRequest, user);
        return ResultUtils.success(result);
    }


    /**
     * 删除空间 本人和管理员
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck
    public BaseResponse<Integer> deleteSpace(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {

        User user = userService.userLoginInfo(request);
        int result = spaceService.deleteSpace(deleteRequest, user);
        return ResultUtils.success(result);
    }

    /**
     * 查看空间级别
     *
     * @return
     */
    @GetMapping("/list/level")
    public BaseResponse<List<SpaceLevel>> listSpaceLevel() {
        // List<SpaceLevel> spaceLevels = new ArrayList<>();
        // for (SpaceLevelEnum spaceLevelEnum : SpaceLevelEnum.values()) {
        //
        //     spaceLevels.add(
        //             SpaceLevel.builder()
        //                     .value(spaceLevelEnum.getValue())
        //                     .text(spaceLevelEnum.getText())
        //                     .maxSize(spaceLevelEnum.getMaxSize())
        //                     .maxCount(spaceLevelEnum.getMaxCount())
        //                     .build()
        //
        //     );
        // }
        // 优化：
        List<SpaceLevel> spaceLevels = Arrays.stream(SpaceLevelEnum.values())
                .map(spaceLevelEnum -> SpaceLevel.builder()
                        .value(spaceLevelEnum.getValue())
                        .text(spaceLevelEnum.getText())
                        .maxSize(spaceLevelEnum.getMaxSize())
                        .maxCount(spaceLevelEnum.getMaxCount())
                        .build())
                .collect(Collectors.toList());

        return ResultUtils.success(spaceLevels);
    }

}
